package com.xmy.cultivate.service.message;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.bean.message.TextMessage;
import com.xmy.cultivate.config.WeCharConfig;
import com.xmy.cultivate.constant.WeCharConstant;
import com.xmy.cultivate.entity.Student;
import com.xmy.cultivate.entity.WecharsBind;
import com.xmy.cultivate.service.IStudentService;
import com.xmy.cultivate.service.impl.WecharsBindServiceImpl;
import com.xmy.cultivate.utils.CheckPhoneHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TextMessageService implements IMessage {

    private final WeCharConfig weCharConfig;

    @Autowired
    WecharsBindServiceImpl wecharsBindService;

    @Autowired
    IStudentService iStudentService;


    public TextMessageService(WeCharConfig weCharConfig) {
        this.weCharConfig = weCharConfig;
    }

    @Override
    public String handler(Map<String, Object> stringObjectMap) {
        //System.out.println("stringObjectMap");
        //System.out.println(stringObjectMap);

        String txtContent = String.valueOf(stringObjectMap.get("Content"));
        String fromUserName = String.valueOf(stringObjectMap.get("FromUserName"));
        String loveHeart = "";
        String content = "家长您好！此平台仅用于发通知提醒和分享教育信息，老师无法看到您发送的信息，有任何问题请在VIP加油站群联系老师"+loveHeart;
        if(CheckPhoneHelper.validatorPhone(txtContent)){
            WecharsBind wecharsBind =new WecharsBind();
            wecharsBind.setOpenid(fromUserName);
            wecharsBind.setPhone(txtContent);
            String rs = wecharsBindService.addPhone(wecharsBind);
            if(StringUtils.isNotBlank(rs)){
                //String studentStr = studentNameList.stream().map(String::valueOf).collect(Collectors.joining(","));
                //content = "绑定成功："+studentStr;
                //System.out.println("=====");
                //System.out.println(content);
                content = rs;
            }else {
                content = "没有找到需要绑定的学生，请确认手机号码是否输入正确或与老师联系";
            }
        }else if(txtContent.equals("all")){
            List<String> studentNameList = wecharsBindService.studentList(fromUserName);
            if(studentNameList.size() > 0){
                String studentStr = studentNameList.stream().map(String::valueOf).collect(Collectors.joining(","));
                content = "已经绑定的学生："+studentStr;
                //System.out.println(content);
            }else {
                content = "没有找到已经绑定的学生";
            }
        }else if(txtContent.equals("cancelbind")){
            boolean rs = wecharsBindService.studentDelete(fromUserName);
            if(rs){
                content = "删除成功";
            }else {
                content = "没有绑定的学生需要删除";
            }

        }

        /*if (stringObjectMap.get(WeCharConstant.CONTENT).toString().equals("登录")) {
            content = WeCharConstant.OAUTH2_AUTHORIZE.replace("APPID", weCharConfig.getAppId())
                    .replace("REDIRECT_URI", "http://192.168.31.123:8080/v1/weChar/getCode").replace("SCOPE", "snsapi_userinfo");
            //System.out.println(content);
            content = "您好，请点击<a href='" + content + "'>登录</a>进行更多的操作。";
        }*/
        TextMessage textMessage = TextMessage.ofSendMsg(stringObjectMap, content);
        return textMessage.toXml();
    }

}
